package com.example.recruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.recruit.common.R;
import com.example.recruit.dto.EmployerCompany;
import com.example.recruit.dto.JobAll;
import com.example.recruit.dto.JobDetail;
import com.example.recruit.entity.Job;
import com.example.recruit.mapper.EmployerMapper;
import com.example.recruit.mapper.JobMapper;
import com.example.recruit.service.EmployerService;
import com.example.recruit.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author WEI
 * 工作类
 * getListJob   获取工作列表
 * updateStatus 修改工作状态
 * deleteJob 根据id删除工作
 * addJob 添加工作
 * employeeAddJobList 查询hr发布的工作
 */
@Slf4j
@RestController
@RequestMapping("/job")
@CrossOrigin(origins = "*")
public class JobController {
    @Autowired
    JobService jobService;

    @Autowired
    JobMapper jobMapper;
    @Autowired
    EmployerService employerService;

    @Autowired
    EmployerMapper employerMapper;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param category
     * @return
     */
    @GetMapping("/findListJob")
    public R<Page> getListJob(int page, int pageSize,String category){
        Page<Job> pageInfo=new Page<>(page,pageSize);
        Page<JobAll> JobAllPage=new Page<>();
        LambdaQueryWrapper<Job> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(category!=null,Job::getCategory,category);
        lambdaQueryWrapper.orderByAsc(Job::getId);
        jobService.page(pageInfo,lambdaQueryWrapper);
        BeanUtils.copyProperties(pageInfo,JobAllPage,"records");
        List<Job> records=pageInfo.getRecords();
        List<JobAll> list=records.stream().map((item)->{
            String bossId=item.getBossId();
            String companyId =item.getCompanyId();
            String jobId=item.getId();
            JobAll jobOne = jobMapper.getJobAllListByBossId(bossId,companyId,jobId);
            return jobOne;

        }).collect(Collectors.toList());
        JobAllPage.setRecords(list);
        return R.success(JobAllPage);
    }


    /**
     * 修改工作招聘状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/")
    public R<Boolean> updateStatus( Integer status,@RequestParam List<String>  id){
        log.info("statusid:"+id+"===>"+status);
        LambdaUpdateWrapper<Job> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Job::getStatus,status);
        lambdaUpdateWrapper.in(Job::getId,id);
        boolean flag = jobService.update(lambdaUpdateWrapper);
        if(flag){
            return   R.success((status==0)? false:true );
        }
        return R.error((status==0)? "停招失败":"起招失败" );
    }

    /**
     * 修改工作
     * @param jobList
     * @return
     */
    @PostMapping("/modifyJob")
    public R<String> modifyJob(@RequestBody List<Job> jobList){
        boolean flag = jobService.updateBatchById(jobList);
        if(flag==true){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }

    }


    /**
     * 根据id删除工作
     *
     * @param job
     * @return
     */
    @PostMapping("/delete")
    public R<String> deleteJob(@RequestBody Job job){
        if(job!=null&&job.getId()!=null){
            int i = jobMapper.deleteById(job.getId());
            if(i>0){
                return R.success("删除成功");
            }

        }
        return R.error("删除失败");
    }


    /**
     * 添加工作
     * @param job
     * @return
     */

    @PostMapping("/addJob")
    public R<Job> addJob(@RequestBody Job job){
        if(job.getBossId()!=null&&job.getCompanyId()!=null){
            int insert = jobMapper.insert(job);
                    if(insert>0){
                        return R.success(job);
                    }
        }
        return R.error("添加失败");
    }

    /**
     * 查询hr发布的工作
     * @param
     * @return
     */
    @GetMapping("/employerAddjob")
    public R<List<Job>> employeeAddJobList(String bossId){
        LambdaUpdateWrapper<Job> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Job::getBossId,bossId);
        List<Job> jobList = jobMapper.selectList(lambdaUpdateWrapper);
        return R.success(jobList);
    }

    @GetMapping("/getDetail")
    public R<JobDetail> getJobDetail(String jobId){
        JobDetail jobDetail = jobMapper.getJobDetailByJobId(jobId);
        return R.success(jobDetail);
    }
}
